//
//  CXSegmentControl.m
//  WangLottery
//
//  Created by 黄修勇 on 12-4-27.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CXSegmentControl.h"

@implementation CXSegmentControl

@synthesize selectIndex=_selectIndex;
@synthesize offsetWidth=_offsetWidth;


-(void)dealloc{

    [itemViews release];
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        itemViews=[NSMutableArray new];
        _offsetWidth=15.0f;
    }
    return self;
}

-(id)initwithItem:(NSArray *)items{

    self=[self initWithFrame:CGRectZero];
    
    if (self) {
        for (int i=0; i<[items count]; i++) {
            CXSegmentItemView *view=[[CXSegmentItemView alloc]initWithFrame:CGRectZero];
            view.titleString=[items objectAtIndex:i];
            [itemViews addObject:view];
            view.tag=i;
            [view addTarget:self action:@selector(itemViewIsTouchUpInSider:) forControlEvents:UIControlEventTouchUpInside];
            [self insertSubview:view atIndex:0];
            if (i==0) {
                view.isSelect=YES;
            }
            [view release];
        }
    }
    return self;
}

-(void)layoutSubviews{

    float tempWidth=self.frame.size.width/itemViews.count+_offsetWidth;
    
    for (int i=0; i<itemViews.count; i++) {
        CXSegmentItemView *view=[itemViews objectAtIndex:i];
        view.frame=CGRectMake(i*tempWidth-i*(_offsetWidth*2), 0.0f, tempWidth, self.frame.size.height);
        if (view.isSelect) {
            [self bringSubviewToFront:view];
        }else {
            [self sendSubviewToBack:view];
        }
        
    }
}


-(void)setOffsetWidth:(float)offsetWidth{

    if (_offsetWidth==offsetWidth) {
        return;
    }
    
    _offsetWidth=offsetWidth;
    [self layoutSubviews];
}

-(void)setSelectIndex:(NSInteger)selectIndex{

    if (selectIndex==_selectIndex) {
        return;
    }
    CXSegmentItemView *lastSelectView=[itemViews objectAtIndex:_selectIndex];
    lastSelectView.isSelect=NO;
    
    CXSegmentItemView *selectView=[itemViews objectAtIndex:selectIndex];
    selectView.isSelect=YES;
    
    [self layoutSubviews];
    
    _selectIndex=selectIndex;
    
}

-(void)itemViewIsTouchUpInSider:(CXSegmentItemView *)view{

    if (view.isSelect) {
        return;
    }
    self.selectIndex=view.tag;
    
    [self sendActionsForControlEvents:UIControlEventValueChanged];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

@interface CXSegmentItemView (Private)

-(void)setDefault;

@end

@implementation CXSegmentItemView

@synthesize isSelect=_isSelect;
@synthesize normalimage=_normalimage;
@synthesize hightedImage=_hightedImage;
@synthesize titleString=_titleString;
@synthesize titleLabelInsets=_titleLabelInsets;

-(void)dealloc{

    [_normalimage release];
    [_hightedImage release];
    [backgroundImageView release];
    [titleLabel release];
    [super dealloc];
}

-(id)initWithFrame:(CGRect)frame{

    self=[super initWithFrame:frame];
    if (self) {
        [self setDefault];
    }
    return self;
}

-(void)setDefault{

    UIImage *tempNormal=[UIImage imageNamed:@"segment_normal.png"];
    self.normalimage=tempNormal;
    UIImage *tempHighted=[UIImage imageNamed:@"segment_highted.png"];
    self.hightedImage=tempHighted;
    
    backgroundImageView=[[UIImageView alloc]initWithImage:_normalimage];
    backgroundImageView.frame=CGRectMake(0.0f, 0.0f, self.frame.size.width, self.frame.size.height);
    backgroundImageView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    [self addSubview:backgroundImageView];
    
    titleLabel=[[UILabel alloc]initWithFrame:backgroundImageView.frame];
    titleLabel.backgroundColor=[UIColor clearColor];
    titleLabel.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
    titleLabel.textColor=[UIColor whiteColor];
    titleLabel.font=[UIFont systemFontOfSize:14.0f];
    [self addSubview:titleLabel];
    titleLabel.textAlignment=UITextAlignmentCenter;
    
    self.titleLabelInsets=UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 25.0f);
    
}

-(void)setTitleString:(NSString *)titleString{

    titleLabel.text=titleString;
}

-(NSString *)titleString{

    return titleLabel.text;
}

-(void)setIsSelect:(BOOL)isSelect{

    if (isSelect==self.isSelect) {
        return;
    }
    
    _isSelect=isSelect;
    
    if (isSelect) {
        backgroundImageView.image=_hightedImage;
        titleLabel.shadowColor=[UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:0.4f];
        titleLabel.shadowOffset=CGSizeMake(0.0f, 1.0f);
    }else {
        backgroundImageView.image=_normalimage;
        titleLabel.shadowColor=[UIColor clearColor];
        titleLabel.shadowOffset=CGSizeMake(0.0f, 0.0f);
    }
}


-(void)setNormalimage:(UIImage *)normalimage{

    if ([normalimage isEqual:_normalimage]) {
        return;
    }
    [_normalimage release];

    _normalimage=[normalimage retain];
    
    if (!self.isSelect) {
        backgroundImageView.image=_normalimage;
    }
}

-(void)setHightedImage:(UIImage *)hightedImage{

    if ([hightedImage isEqual:_hightedImage]) {
        return;
    }
    
    [_hightedImage release];
    _hightedImage=[hightedImage retain];
    
    if (self.isSelect) {
        backgroundImageView.image=_hightedImage;
    }
}

-(void)setTitleLabelInsets:(UIEdgeInsets)titleLabelInsets{

    if (UIEdgeInsetsEqualToEdgeInsets(titleLabelInsets, _titleLabelInsets)) {
        return;
    }
    
    _titleLabelInsets=titleLabelInsets;
    CGRect titleLabelFrame=UIEdgeInsetsInsetRect(titleLabel.frame, titleLabelInsets);
    titleLabel.frame=titleLabelFrame;
}
@end
